<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)"/>

    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="picCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="picUrl" :src="picUrl" @click="refreshPic" alt="">
        </div>
        <div class="form-item">
          <input v-model="msgCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode">
            {{ second === totalSecond ? '获取验证码' : second + '秒后重新发送' }}
          </button>
        </div>
      </div>

      <div @click="login" class="login-btn">登录</div>
    </div>
  </div>
</template>

<script>
import { getPicCode, getMsgCode, codeLogin } from '@/api/login'
import { Toast } from 'vant'
import { mapMutations } from 'vuex'

export default {
  name: 'LoginPage',
  data () {
    return {
      picKey: '', // 将来请求传递的图形验证码唯一标识
      picUrl: '', // 存储请求渲染的图片地址
      totalSecond: 5, // 总秒数
      second: 5, // 当前秒数，开定时器对 second--
      timer: null, // 定时器 id
      mobile: '', // 手机号
      picCode: '', // 用户输入的图形验证码
      msgCode: '', // 短信验证码
      mockMsgCode: ''
    }
  },
  created () {
    this.refreshPic()
  },
  methods: {
    refreshPic () {
      getPicCode().then(res => {
        Toast('获取验证码成功')
        this.picKey = res.data.key
        this.picUrl = res.data.base64
      })
    },
    validate () {
      if (!this.mobile) {
        Toast('请输入手机号')
        return false
      }
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        Toast('手机号格式不正确')
        return false
      }
      if (!this.picCode) {
        Toast('请输入图形验证码')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        Toast('图形验证码格式不正确')
        return false
      }
      return true
    },
    getCode () {
      if (!this.validate()) {
        return
      }
      console.log(this.timer, this.second, this.totalSecond)
      // timer未开启，并且当前秒数等于总秒数，开启定时器
      if (!this.timer && this.second === this.totalSecond) {
        this.sendMsg()
        this.timer = setInterval(() => {
          this.second--
          // 当前秒数小于等于0，关闭定时器
          if (this.second <= 0) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
          }
        }, 1000)
      }
    },
    sendMsg () {
      getMsgCode(this.picCode, this.picKey, this.mobile).then(res => {
        Toast('发送成功')
        this.mockMsgCode = res.message.split(':').pop().trim()
      })
    },
    login () {
      if (!this.validate()) {
        return
      }
      if (!/^\w{6}$/.test(this.msgCode)) {
        Toast('短信验证码格式不正确')
        return
      }
      codeLogin(this.mobile, this.msgCode).then(res => {
        Toast('登录成功')
        this.setUserInfo(res.data)
        const backUrl = this.$route.query.backUrl || '/'
        this.$router.push(backUrl)
      }).catch(err => {
        this.$toast(err)
      })
    },
    ...mapMutations('user', ['setUserInfo'])
  },
  destroyed () {
    clearInterval(this.timer)
    this.timer = null
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;

    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }

    img {
      width: 94px;
      height: 31px;
    }

    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
